import { Controller } from "../../mvc/core/Controller";
import { EatParadePanel } from "../view/eatParade/EatParadePanel";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { DataEvent } from "../events/DataEvent";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { Alert } from "../../../com/mui/controls/Alert";
import { GameProxy } from "../mvc/GameProxy";
import { Global } from "../../../com/gengine/global/Global";
import { SEatParadeActiveStartInfo } from "../../../Message/Public/SEatParadeActiveStartInfo";
import { Game } from "../Game";
import { MapFileUtil } from "../scene/map/MapFileUtil";
import { SNpcShowControl } from "../../../Message/Public/SNpcShowControl";
import { SecTimer } from "../../../com/gengine/core/frame/SecTimer";
import { TimerType } from "../../../com/gengine/core/frame/TimerType";
type int = number;
//class EatParadeActiveController
    
    export  class EatParadeActiveController extends Controller
    {
       

        /* internal  */get topView(): EatParadePanel
        {
            if (!this._topView) 
            {
                this._topView = new EatParadePanel();
                this._topView.hide();
            }
            return this._topView;
        }

        protected /* override */ initServer(): void
        {
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicEatParadeActiveNPCControl, this.npcIsShow);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicEatParadeActiveStart, this.activeIsStart);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicEatParadeActiveEnd, this.applyEnd);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicEatParadeEndEachRing, this.currentEnd);
            Dispatcher.addEventListener(EventName.GetEatParadeInfo, this.getActiveInfo);
            Dispatcher.addEventListener(EventName.GetEatParadeInfoSuccess, this.getActiveInfoSuccess);
            Dispatcher.addEventListener(EventName.EatParadeApply, this.onEatParadeApply);
            Dispatcher.addEventListener(EventName.EatParadeApplySuccess, this.onEatParadeApplySuccess);
            Dispatcher.addEventListener(EventName.LoginGameSuccess, this.onLoginGameSuccessHandler);
            Dispatcher.addEventListener(EventName.BreakEatParade, this.breakEatParadeActive);
            Dispatcher.addEventListener(EventName.BreakEatParadeSuccess, this.breakEatParadeActiveSuccess);
            return;
        }

        /* internal  */currentEnd(arg1: MessageBlock): void
        {
            this._playerApply = false;
            this.topView.hide();
            return;
        }

        /* internal  */breakEatParadeActiveSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                this.topView.clear();
                this.topView.hide();
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(200105));
            }
            return;
        }

        /* internal  */breakEatParadeActive(arg1: DataEvent): void
        {
            var e: DataEvent;
            var onClickHandler: Function;

            var loc1;
            onClickHandler = null;
            e = arg1;
            onClickHandler = (arg1: int): void
            {
                if (arg1 == Alert.YES) 
                {
                    GameProxy.active.breakEatParade();
                    Alert.resetToDefault();
                }
                return;
            }
            Alert.show(Language.getString(200107), null, Alert.YES | Alert.NO, null, onClickHandler);
            return;
        }

        /* internal  */onEatParadeApply(arg1: DataEvent): void
        {
            GameProxy.active.onEatParadeApply();
            return;
        }

        /* internal  */onEatParadeApplySuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            this.topView.show((Global.stage.stageWidth - this.topView.width) / 2, 150);
            this._playerApply = true;
            if (loc1 > 0) 
            {
                this.topView.setTime(loc1, false);
            }
            return;
        }

        /* internal  */activeIsStart(arg1: MessageBlock): void
        {
            cache.dialog.eatParadeApplyProcess = 1;
            Dispatcher.dispatchEvent(new DataEvent(EventName.EatParadeProcess, 1));
            this.onLoginGameSuccessHandler(null);
            GameProxy.active.getEatParadeInfo();
            return;
        }

        /* internal  */applyEnd(arg1: MessageBlock): void
        {
            cache.dialog.eatParadeApplyProcess = 0;
            this.clear();
            Dispatcher.dispatchEvent(new DataEvent(EventName.EatParadeProcess, 0));
            Dispatcher.dispatchEvent(new DataEvent(EventName.OnEatParadeStartTimeCount, 0));
            if (this._playerApply) 
            {
                this.topView.clear();
                this.topView.show((Global.stage.stageWidth - this.topView.width) / 2, 150);
                this.topView.setProcessBitmap(this._playerApply);
            }
            return;
        }

        /* internal  */getActiveInfoSuccess(arg1: DataEvent): void
        {
            var loc1=arg1.data as SEatParadeActiveStartInfo;
            cache.dialog.eatParadeInfo = loc1;
            this.setTime(loc1.leftSeconds);
            if (cache.dialog.npcInfo && cache.dialog.npcInfo.tnpc.npcId == 5010012) 
            {
                cache.dialog.updateNpcOperate(cache.dialog.npcInfo.tnpc.effectDes);
            }
            return;
        }

        /* internal  */getActiveInfo(arg1: DataEvent): void
        {
            var loc1=arg1.data as Boolean;
            if (loc1) 
            {
                GameProxy.active.getEatParadeInfo();
            }
            return;
        }

        /* internal  */onLoginGameSuccessHandler(arg1: DataEvent): void
        {
            var loc1=0;
            while (loc1 < 2) 
            {
                this.updateNpc(5010013 + loc1, false);
                ++loc1;
            }
            return;
        }

        /* internal  */updateNpc(arg1: int, arg2: Boolean): void
        {
            Game.scene.updateNpcIsAddToStage(arg1, arg2, 400101);
            if (400101 == MapFileUtil.mapID) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.MapUINPCShowOrHide, {"npcId": arg1, "show": arg2}));
            }
            return;
        }

        /* internal  */npcIsShow(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SNpcShowControl;
            this.showOrHideEatParadeNpc(loc1.npcId, loc1.isShow);
            return;
        }

        /* internal  */showOrHideEatParadeNpc(arg1: int, arg2: Boolean): void
        {
            this._isShow = arg2;
            this._npcId = arg1;
            if (arg2) 
            {
                Dispatcher.dispatchEvent(new DataEvent(EventName.EatParadeProcess, cache.dialog.eatParadeApplyProcess));
            }
            if (Game.isSceneInit()) 
            {
                this.onSceneUpdate(null);
            }
            else 
            {
                Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdate);
            }
            return;
        }

        /* internal  */onSceneUpdate(arg1: DataEvent): void
        {
            Dispatcher.removeEventListener(EventName.Scene_Update, this.onSceneUpdate);
            this.updateNpc(this._npcId, this._isShow);
            return;
        }

        /* internal  */setTime(arg1: int): void
        {
            this._time = arg1;
            this.startCountDown();
            return;
        }

        /* internal  */startCountDown(): void
        {
            if (!this._timer) 
            {
                this._timer = new SecTimer();
                this._timer.addListener(TimerType.ENTERFRAME, this.onTimerHandler);
                this._timer.start();
            }
            return;
        }

        /* internal  */onTimerHandler(arg1: SecTimer): void
        {
            --this._time;
            Dispatcher.dispatchEvent(new DataEvent(EventName.OnEatParadeStartTimeCount, this._time));
            return;
        }

        /* internal  */clear(): void
        {
            if (this._timer) 
            {
                this._timer.stop();
                this._timer.dispose();
                this._timer = null;
            }
            return;
        }

        private /* var */_timer: SecTimer;

        private /* var */_time: int;

        private /* var */_topView: EatParadePanel;

        private /* var */_playerApply: Boolean=false;

        private /* var */_isShow: Boolean=false;

        private /* var */_npcId: int;
    }
